<?php
    class admin_Ajax extends Ajax
    {
        /**
        * Bien chung
        */
        public $cataloggeneral ='cataloggeneral';

        /**
        * Hang Muc
        */
        public function addCatalogy_general()
        {
            $oDatabase = new database();
            // Get id lastest in settingid table
            $lastcatalog = $oDatabase->getObject('select *from settingid where tablename = "cataloggeneral"');
            // id = id +1 return id type : tablename_id;
            $iCatalog_id = core::getID($lastcatalog->lastest_id,$lastcatalog->tableName);
            // Set array  
            $aData = array(
                'mahm_id' => $iCatalog_id,
                'tenhm' => $_POST['catalog']
            );
            $result = $oDatabase->add('hangmuc',$aData); 
            // update ID in settingid table (tablename property, id)
            core::updateSettingID('cataloggeneral',((int)$lastcatalog->lastest_id+1));
            if($result)
            {
                echo $iCatalog_id;
            }
            else
            {
                echo "Fail";
            }
        }
        
        /**
        * Function update Catalogygenerral (HangMuc)
        * 
        */
        public function updateCatalogy_general()
        {  
            $oDatabase = new database();
            $aCondition = array(
                'mahm_id = "'.$_POST['IDcatalog'] => '"',

            );
            $aData = array(
                'tenhm' => $_POST['catalog']    
            );

            $result = $oDatabase->update('hangmuc',$aData, $aCondition);
            if($result)
            {
                echo "OK";
            }
            else
            {
                echo $oDatabase->sSql;
            }

        }
        
        /**
        * Function delete Catalogygenerral (HangMuc)
        * 
        */
        public function deleteCatalogy_general()
        {
            $oDatabase = new database();
            $aCondition = array(
                'mahm_id = "'.$_POST['IDcatalog'] => '"',

            );
            $result = $oDatabase->delete('hangmuc', $aCondition);
            if($result)
            {
                echo "OK";
            }
            else
            {
                echo $oDatabase->sSql;
            }
        }
        
        /**
        * Function add Calalogy (DanhMuc)
        * 
        */
        public function addCatalogy()
        {
            $oDatabase = new database();
            // Get id lastest in settingid table
            $lastcatalog = $oDatabase->getObject('select *from settingid where tablename = "catalog"');
            // id = id +1 return id type : tablename_id;
            $iCatalog_id = core::getID($lastcatalog->lastest_id,$lastcatalog->tableName);
            // Set array
            $aData = array(
                'madm_id' => $iCatalog_id,
                'tendm' => $_POST['catalog'],
            );
            $result = $oDatabase->add('danhmuc',$aData); 
            // update ID in settingid table (tablename property, id)
            core::updateSettingID('catalog',((int)$lastcatalog->lastest_id+1));
            if($result)
            {
                echo $iCatalog_id;

                // Create rdf resources
                $ns = array(
                    'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                    'foaf' => 'http://xmlns.com/foaf/0.1/',
                    'bus' => URL::getLink('id/bussiness#'),
                    'xsd' => 'http://www.w3.org/2001/XMLSchema#',
                );
				$uri = URL::getLink("category/id/".$iCatalog_id);
                $index = array(
                    $uri => array(
                        "rdf:type" => array( 
                            array( "value" => URL::getLink("id/bussiness#category"), 
                                "type" => "uri" ) 
                        ) ,
                        "bus:name" => array(
                            array( "value" => $_POST['catalog'], 
                                "type" => "literal"),
                        ),
                        "bus:defaultImage" => array(
                            array( "value" => URL::getLink('upload/'.$_POST['imageUrl']), 
                                "type" => "literal"),
                        ),
                        "bus:image" => array(
                            array( "value" => URL::getLink('upload/'.$_POST['imageUrl']), 
                                "type" => "literal"),
                        ),
                    ) 
                );
                if($_POST['bIsSubCategory'] == 'true')
                {
                    // split sParentCategory into array Categories
                    $str = chop($_POST['sParentCategories']);
                    $sCategories = explode(';', $str);
                    $aCategories = array();
                    foreach($sCategories as $sCategory)
                    {
                        $aTemp = array( "value" => $sCategory ,"type" => "uri");
                        array_push($aCategories , $aTemp);
                    }

                    $index[$uri]['bus:childCategoryOf'] = $aCategories;

                    $index[$uri]['bus:subCategory'] = array(
                        array(  "value" => $_POST['sParentCategory'] ,"type" => "uri"),
                    );
                }
                $oDatabase->createResources($ns , $index , $iCatalog_id ,'category');
            }
            else
            {
                echo "Fail";
            }
        }
        
        /**
        * Function update Calalogy (DanhMuc)
        * 
        */
        public function updateCatalogy()
        {  
            $oDatabase = new database();
            $aCondition = array(
                'madm_id = "'.$_POST['IDcatalog'] => '"',

            );

            $aData = array(
                'mahm_id' => $_POST['idcataloggen'],    
                'tendm' => $_POST['catalog']    
            );

            $result = $oDatabase->update('danhmuc',$aData, $aCondition);
            if($result)
            {
                echo "OK";
            }
            else
            {
                echo $oDatabase->sSql;
            }

        }
        
        /**
        * Function delete Calalogy (DanhMuc)
        * 
        */
        public function deleteCatalogy()
        {
            $oDatabase = new database();
            $aCondition = array(
                'madm_id = "'.$_POST['IDcatalog'] => '"',

            );
            $result = $oDatabase->delete('danhmuc', $aCondition);
            if($result)
            {
                echo "OK";
            }
            else
            {
                echo $oDatabase->sSql;
            }
        }

        /**
        * Function delete HoTProduct 
        * 
        */
        public function deleteHotProduct()
        {
            $oDatabase = new database();
            $aCondition = array(
                'masp_id = "'.trim($_POST['IDcatalog']) => '"',

            );
            $result = $oDatabase->delete('sanphamhot', $aCondition);
            if($result)
            {
                echo "OK";
            }
            else
            {
                echo $oDatabase->sSql;
            }
        }
        
        /**
        * Function add HoTProduct 
        * 
        */
        public function addHotProduct()
        {
            $oDatabase = new database();
            $aID=split("/id/",$_POST['idProduct']);
            $aData = array(
                'masp_id' => trim($aID[1]),
            );
            $result = $oDatabase->add('sanphamhot',$aData); 
            if($result)
            {
                echo $result;
            }
            else
            {
                echo "Fail";
            }
        }
        
        /**
        * Function check HoTProduct exits
        * 
        */
        public function checkAddHotProduct()
        {
            $oDatabase = new database();
            $aID=split("/id/",$_POST['idProduct']);
            $result = $oDatabase->getObject('select *from sanphamhot where masp_id ="'.trim($aID[1]).'"');
            if($result)
            {
                echo $result ->masp;
            }
            else
            {
                echo "Fail";
            }
        }
        /**
        * Code ajax  manufacturer ( nha San Xuat)
        */

        public function addManufacturer()
        {
            $oDatabase = new database();
            // Get id lastest in settingid table
            $lastmanufacturer = $oDatabase->getObject('SELECT * FROM settingid WHERE tablename = "manufacturer"');
            // id = id +1 return id type : tablename_id;
            $manufacturerID = core::getID($lastmanufacturer->lastest_id,$lastmanufacturer->tableName);
            // Set array  
            $aData = array(
                'manufacturerid' => $manufacturerID,
                'name' => $_POST['name']
            );
            $result = $oDatabase->add('manufacturer',$aData); 
            // update ID in settingid table (tablename property, id)
            core::updateSettingID('manufacturer',((int)$lastmanufacturer->lastest_id+1));
            if($result)
            {
                $aNameSpace = array(
                    'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                    'bus' => URL::getLink('id/bussiness#'),
                );
                $uri= URL::getLink('id/'.$manufacturerID);
                $data = array(
                    $uri => array(
                        "rdf:type" => array( 
                            array( "value" => URL::getLink("id/bussiness#manufacturer"), 
                                "type" => "uri" ) 
                        ) ,
                        "bus:name" => array(
                            array( "value" => $_POST['name'], 
                                "type" => "literal")
                        ),
                        "bus:description" => array(
                            array( "value" => $_POST['description'], 
                                "type" => "literal")
                        ),
                        "bus:hasCategory" => array(
                            array( "value" => $_POST['category'], 
                                "type" => "uri")
                        )
                    ) 
                );
                $oDatabase ->createResources($aNameSpace,$data,$manufacturerID,'');
                echo $manufacturerID;
            }
            else
            {
                echo "FAIL";
            }
        }
        
        /**
        * Function update manufacturer (Nha San Xuat)
        * 
        */
        public function updateManufacturer()
        {  
            $oDatabase = new database();
            $aCondition = array(
                'manufacturerid = "'.$_POST['IDmanufacturer'] => '"',

            );
            $aData = array(
                'name' => $_POST['namemanufacturer']    
            );

            $result = $oDatabase->update('manufacturer',$aData, $aCondition);
            if($result)
            {
                 $aNameSpace = array(
                    'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
                    'bus' => URL::getLink('id/bussiness#'),
                );
                $manufacturerID = end(explode('/id/',$_POST['IDmanufacturer']));
                $uri=URL::getLink('id/'.$manufacturerID);
                $data = array(
                    $uri => array(
                        "rdf:type" => array( 
                            array( "value" => URL::getLink("id/bussiness#manufacturer"), 
                                "type" => "uri" ) 
                        ) ,
                        "bus:name" => array(
                            array( "value" => $_POST['namemanufacturer'], 
                                "type" => "literal")
                        ),
                        "bus:description" => array(
                            array( "value" => $_POST['description'], 
                                "type" => "literal")
                        )
                    ) 
                );
                $oDatabase ->updateResources($aNameSpace,$data,$manufacturerID,'');
                echo "OK";
            }
            else
            {
                echo $oDatabase->sSql;
            }

        }
        
        /**
        * Function delete manufacturer (Nha San Xuat)
        * 
        */
        public function deleteManufacturer()
        {
            $oDatabase = new database();
            $aCondition = array(
                'manufacturerid = "'.$_POST['IDmanufacturer'] => '"',

            );
            $result = $oDatabase->delete('manufacturer', $aCondition);
            if($result)
            {
                echo "OK";
            }
            else
            {
                echo $oDatabase->sSql;
            }
        }    
        
        public function loadImageOfCategory()
        {
            $aParams = array(
                'uri' => $_POST['productUri'],
            );
            includeBlock('block/imageBlock',$aParams);
        } 
        
        public function setDefaultCategoryLayout()
        {
            if(isset($_POST['uri']))
            {
                $oDatabase = new database();
                $oDatabase->setProperity($_POST['uri'],'bus:defaultProductLayout',$_POST['value']);
            }
        }
        public function updateProperty()
        {
            if(isset($_POST['uri']) && isset($_POST['property']) && isset($_POST['value']))
            {
                $oDatabase = new database();
                $oDatabase->setProperity($_POST['uri'],$_POST['property'],$_POST['value']);
            }
        }
    }
?>
